package cn.linkpower.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	private Logger log = LoggerFactory.getLogger(TestController.class);
	
	private static final String PATH_PARAMETER_SPLIT = "_";
	
	@Autowired
	private MessageSource messageSource;
	
	public String getMessage(String key,HttpServletRequest request,String... strings ) {
		String language = request.getParameter("language");
		log.info("language:{}",language);
		//获取请求头默认的local对象
		Locale locale = request.getLocale();
		log.info("Add default.");
		if(!StringUtils.isEmpty(language)) {
			//按照指定的正则表达式，解析出相关的数据信息
			String[] split = language.split(PATH_PARAMETER_SPLIT);
			//解析出数据后，修改local对象
			locale = new Locale(split[0], split[1]);
			log.info("Add custom.");
		}
		return this.messageSource.getMessage(key, strings,locale);
	}
	
	/**
	 * en_US <br>
	 * zh_CN
	 * @param request
	 * @return
	 */
	@RequestMapping("/test1")
	public String test1(HttpServletRequest request) {
		//log.info("local:{}",Locale.getDefault().getLanguage());
		return getMessage("test.name",request,null);
	}
}
